#include "stdafx.h"

#define CINTERFACE
#include "HookD3D.h"

#include <stdio.h>

template<class iface, int offset, typename typefunc>
BOOL CComHooker<iface, offset, typefunc>::Hook()
{
	DWORD dwProtectOld;
	PPROC& p = GetProcAddr();
	if (!VirtualProtect(&p, sizeof(p), PAGE_EXECUTE_READWRITE, &dwProtectOld))
		return FALSE;
	m_pfnOld = p;
	p = m_pfnNew;
	VirtualProtect(&p, sizeof(p), dwProtectOld, &dwProtectOld);
	m_bHooked = TRUE;
	return TRUE;
}

template<class iface, int offset, typename typefunc>
BOOL CComHooker<iface, offset, typefunc>::Unhook()
{
	PPROC& p = GetProcAddr();
	DWORD dwProtectOld;
	if (!VirtualProtect(&p, sizeof(p), PAGE_EXECUTE_READWRITE, &dwProtectOld))
		return FALSE;
	p= m_pfnOld;
	VirtualProtect(&p, sizeof(p), dwProtectOld, &dwProtectOld);
	m_bHooked = FALSE;
	return TRUE;
}

typedef HRESULT STDMETHODCALLTYPE HOOK_FUNC(IDirect3DSwapChain9, Present)(IDirect3DSwapChain9* This, CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion,DWORD dwFlags);
DEFINE_HOOK(IDirect3DSwapChain9, Present)

DEFINE_COMHOOKER(IDirect3DSwapChain9, Present)
DEFINE_COMHOOKER(IDirect3DDevice9, DrawPrimitive)
DEFINE_COMHOOKER(IDXGISwapChainDWM, Present)
DEFINE_COMHOOKER(ID3D10Device, DrawIndexed)
DEFINE_COMHOOKER(ID3D10Device, Draw)